/**
 * layout - jQuery EasyUI
 * 
 * Copyright (c) 2009-2013 www.jeasyui.com. All rights reserved.
 *
 * Licensed under the GPL or commercial licenses
 * To use it on other terms please contact us: info@jeasyui.com
 * http://www.gnu.org/licenses/gpl.txt
 * http://www.jeasyui.com/license_commercial.php
 * 
 * Dependencies:
 *   resizable
 *   panel
 */
(function($){
	var resizing = false;	// indicate if the region panel is resizing
	
	function setSize(container){
		var state = $.data(container, 'layout');
		var opts = state.options;
		var panels = state.panels;
		
		var cc = $(container);
		if (container.tagName == 'BODY'){
			cc._fit();
		} else {
			opts.fit ? cc.css(cc._fit()) : cc._fit(false);
		}
		
//		opts.fit ? cc.css(cc._fit()) : cc._fit(false);
//		if (opts.fit == true){
//			var p = cc.parent();
//			p.addClass('panel-noscroll');
//			if (p[0].tagName == 'BODY') $('html').addClass('panel-fit');
//			cc.width(p.width());
//			cc.height(p.height());
//		}
		
		var cpos = {
			top:0,
			left:0,
			width:cc.width(),
			height:cc.height()
		};
		
		setVSize(isVisible(panels.expandNorth) ? panels.expandNorth : panels.north, 'n');
		setVSize(isVisible(panels.expandSouth) ? panels.expandSouth : panels.south, 's');
		setHSize(isVisible(panels.expandEast) ? panels.expandEast : panels.east, 'e');
		setHSize(isVisible(panels.expandWest) ? panels.expandWest : panels.west, 'w');
		
		panels.center.panel('resize', cpos);
		
		function getPanelHeight(pp){
			var opts = pp.panel('options');
			return Math.min(Math.max(opts.height, opts.minHeight), opts.maxHeight);
		}
		function getPanelWidth(pp){
			var opts = pp.panel('options');
			return Math.min(Math.max(opts.width, opts.minWidth), opts.maxWidth);
		}
		function setVSize(pp, type){
			if (!pp.length){return}
			var opts = pp.panel('options');
			var height = getPanelHeight(pp);
            var ccWidth = cc.width();
            var ccHeight = cc.height();
			pp.panel('resize', {
				width: ccWidth,
				height: height,
				left: 0,
				top: (type=='n' ? 0 : ccHeight-height)
			});
			cpos.height -= height;
			if (type == 'n'){
				cpos.top += height;
			}

            //存在splitImg的话就重新设置位置
            var proxyImg = panels['layout-split-img-' + ((type == 'n') ? 'north' : 'south')];
            proxyImg.css({
                //width: ccWidth,
                left: Math.floor((ccWidth - 35) / 2),
                top: (type == 'n') ? (height - 5) : (ccHeight - height)
            });
		}
		function setHSize(pp, type){
			if (!pp.length){return}
			var opts = pp.panel('options');
			var width = getPanelWidth(pp);
			pp.panel('resize', {
				width: width,
				height: cpos.height,
				left: (type=='e' ? cc.width()-width : 0),
				top: cpos.top
			});
			cpos.width -= width;
			if (type == 'w'){
				cpos.left += width;
			}

            //存在splitImg的话就重新设置位置
            var proxyImg = panels['layout-split-img-' + ((type == 'e') ? 'east' : 'west')];
            proxyImg.css({
                left: (type == 'e') ? (cc.width() - width) : (width - 5),
                top: cpos.top + Math.floor((cpos.height - 35) / 2)
            });
		}
	}
	
	/**
	 * initialize and wrap the layout
	 */
	function init(container){
		var cc = $(container);
		
//		if (cc[0].tagName == 'BODY'){
//			$('html').addClass('panel-fit');
//		}
		cc.addClass('layout');
		
		function _add(cc){
			cc.children('div').each(function(){
				var opts = $.fn.layout.parsePanelOptions(this);
				if ('north,south,east,west,center'.indexOf(opts.region) >= 0){
					addPanel(container, opts, this);
				}
			});
		}
		
		cc.children('form').length ? _add(cc.children('form')) : _add(cc);
		
		cc.append('<div class="layout-split-proxy-h"></div><div class="layout-split-proxy-v"></div>');
		
		cc.bind('_resize', function(e,force){
			var opts = $.data(container, 'layout').options;
			if (opts.fit == true || force){
				setSize(container);
			}
			return false;
		});
	}
	
	/**
	 * Add a new region panel on specified element
	 */
	function addPanel(container, param, el){
		param.region = param.region || 'center';
		var panels = $.data(container, 'layout').panels;
		var cc = $(container);
		var dir = param.region;
		
		if (panels[dir].length) return;	// the region panel is already exists
		
		var pp = $(el);
		if (!pp.length){
			pp = $('<div></div>').appendTo(cc);	// the predefined panel isn't exists, create a new panel instead
		}
		
		var popts = $.extend({}, $.fn.layout.paneldefaults, {
			width: (pp.length ? parseInt(pp[0].style.width) || pp.outerWidth() : 'auto'),
			height: (pp.length ? parseInt(pp[0].style.height) || pp.outerHeight() : 'auto'),
			doSize: false,
			collapsible: true,
			cls: ('layout-panel layout-panel-' + dir),
			bodyCls: 'layout-body',
			onOpen: function(){
				var tool = $(this).panel('header').children('div.panel-tool');
				tool.children('a.panel-tool-collapse').hide();	// hide the old collapse button
				
				var buttonDir = {north:'up',south:'down',east:'right',west:'left'};
				if (!buttonDir[dir]) return;
				
				var iconCls = 'layout-button-' + buttonDir[dir];
				// add collapse tool to panel header
				var t = tool.children('a.' + iconCls);
				if (!t.length){
					t = $('<a href="javascript:void(0)"></a>').addClass(iconCls).appendTo(tool);
					t.bind('click', {dir:dir}, function(e){
						collapsePanel(container, e.data.dir);
						return false;
					});
				}
				$(this).panel('options').collapsible ? t.show() : t.hide();
			}
		}, param);
		
		pp.panel(popts);	// create region panel
		panels[dir] = pp;
		
		if (pp.panel('options').split){
			var panel = pp.panel('panel');
			panel.addClass('layout-split-' + dir);
            
            if (pp.panel('options').splitImg) {
                var collapsed = pp.panel('options').collapsed;
                $(container).append('<div class=layout-split-img-' + dir + '></div>');
                var proxyImg = $('>div.layout-split-img-' + dir, container);
                //将split的拖动图片所在的DIV也存储到panels里面，不过这个DIV并非一个panel实例
                panels['layout-split-img-' + dir] = proxyImg;
                var splitImgPos = {
                    'position': 'absolute',
                    'display' : 'block',
                    'z-index' : 4
                };
                if (dir == 'north' || dir == 'south') {
                    splitImgPos.height = 5;
                    splitImgPos.width = 35;
                    if(dir == 'north'){
                        proxyImg.addClass((collapsed==true)?"layout-split-down":"layout-split-up");
                    }else{
                        proxyImg.addClass((collapsed==true)?"layout-split-up":"layout-split-down");
                    }
                } else {
                    splitImgPos.width = 5;
                    splitImgPos.height = 35;
                    if(dir == 'east'){
                        proxyImg.addClass((collapsed==true)?"layout-split-left":"layout-split-right");
                    }else{
                        proxyImg.addClass((collapsed==true)?"layout-split-right":"layout-split-left");
                    }
                }
                proxyImg.css(splitImgPos);
                proxyImg.bind('click.layout',function(e){
                    var methodName;
                    if(param.region=='north'){
                        methodName = $(this).hasClass('layout-split-up')?'collapse':'expand';
                    }else if(param.region=='south'){
                        methodName = $(this).hasClass('layout-split-up')?'expand':'collapse';
                    }else if(param.region=='east'){
                        methodName = $(this).hasClass('layout-split-right')?'collapse':'expand';
                    }else if(param.region=='west'){
                        methodName = $(this).hasClass('layout-split-right')?'expand':'collapse';
                    }
                    $(container).layout(methodName,param.region);
                    return false;
                });
            }
			
			var handles = '';
			if (dir == 'north') handles = 's';
			if (dir == 'south') handles = 'n';
			if (dir == 'east') handles = 'w';
			if (dir == 'west') handles = 'e';
			
			panel.resizable($.extend({}, {
				handles:handles,
				onStartResize: function(e){
					resizing = true;
					
					if (dir == 'north' || dir == 'south'){
						var proxy = $('>div.layout-split-proxy-v', container);
					} else {
						var proxy = $('>div.layout-split-proxy-h', container);
					}
					var top=0,left=0,width=0,height=0;
					var pos = {display: 'block'};
					if (dir == 'north'){
						pos.top = parseInt(panel.css('top')) + panel.outerHeight() - proxy.height();
						pos.left = parseInt(panel.css('left'));
						pos.width = panel.outerWidth();
						pos.height = proxy.height();
					} else if (dir == 'south'){
						pos.top = parseInt(panel.css('top'));
						pos.left = parseInt(panel.css('left'));
						pos.width = panel.outerWidth();
						pos.height = proxy.height();
					} else if (dir == 'east'){
						pos.top = parseInt(panel.css('top')) || 0;
						pos.left = parseInt(panel.css('left')) || 0;
						pos.width = proxy.width();
						pos.height = panel.outerHeight();
					} else if (dir == 'west'){
						pos.top = parseInt(panel.css('top')) || 0;
						pos.left = panel.outerWidth() - proxy.width();
						pos.width = proxy.width();
						pos.height = panel.outerHeight();
					}
					proxy.css(pos);
					
					$('<div class="layout-mask"></div>').css({
						left:0,
						top:0,
						width:cc.width(),
						height:cc.height()
					}).appendTo(cc);
				},
				onResize: function(e){
					if (dir == 'north' || dir == 'south'){
						var proxy = $('>div.layout-split-proxy-v', container);
						proxy.css('top', e.pageY - $(container).offset().top - proxy.height()/2);
					} else {
						var proxy = $('>div.layout-split-proxy-h', container);
						proxy.css('left', e.pageX - $(container).offset().left - proxy.width()/2);
					}
					return false;
				},
				onStopResize: function(e){
//					$('>div.layout-split-proxy-v', container).css('display','none');
//					$('>div.layout-split-proxy-h', container).css('display','none');
//					var opts = pp.panel('options');
//					opts.width = panel.outerWidth();
//					opts.height = panel.outerHeight();
//					opts.left = panel.css('left');
//					opts.top = panel.css('top');
//					pp.panel('resize');
					
					cc.children('div.layout-split-proxy-v,div.layout-split-proxy-h').hide();
					pp.panel('resize',e.data);
					
					setSize(container);
					resizing = false;
					
					cc.find('>div.layout-mask').remove();
				}
			}, param));
		}
	}
	
	/**
	 * remove a region panel
	 */
	function removePanel(container, region){
		var panels = $.data(container, 'layout').panels;
		if (panels[region].length){
			panels[region].panel('destroy');
			panels[region] = $();
			var expandP = 'expand' + region.substring(0,1).toUpperCase() + region.substring(1);
			if (panels[expandP]){
				panels[expandP].panel('destroy');
				panels[expandP] = undefined;
			}
            if(panels['layout-split-img-' + region]){
                panels['layout-split-img-' + region].remove();
                panels['layout-split-img-' + region] = undefined;
            }
		}
	}
	
	function collapsePanel(container, region, animateSpeed){
		if (animateSpeed == undefined){animateSpeed = 'normal';}
		var panels = $.data(container, 'layout').panels;
		
		var p = panels[region];
		var popts = p.panel('options');
		if (popts.onBeforeCollapse.call(p) == false) return;
		
		// expand panel name: expandNorth, expandSouth, expandWest, expandEast
		var expandP = 'expand' + region.substring(0,1).toUpperCase() + region.substring(1);
		if (!panels[expandP]){
			panels[expandP] = createExpandPanel(region);
			panels[expandP].panel('panel').bind('click', function(){
                var proxyImg = $('>div.layout-split-img-' + region, container);
                proxyImg.css({'zIndex':-1});
				var copts = getOption();
				p.panel('expand',false).panel('open').panel('resize', copts.collapse);
                
                var borderStyleObj = {},positionObj = {};
                if(region=='north'){
                    borderStyleObj.borderBottomWidth = 0;
                    positionObj.top = 0;
                }else if(region=='south'){
                    borderStyleObj.borderTopWidth = 0;
                    //positionObj.height = popts.height;
                    positionObj.top = copts.expand.top;
                }else if(region=='east'){
                    borderStyleObj.borderLeftWidth = 0;
                    //positionObj.width = popts.width;
                    positionObj.left = copts.expand.left;
                }else{
                    borderStyleObj.borderRightWidth = 0;
                    positionObj.left = 0;
                }
				
                //针对east和south把边框去掉
                p.panel('panel').css(borderStyleObj);
                if(region=='east'){
                    p.panel('resize',{width:popts.width});
                }else if(region=='south'){
                   p.panel('resize',{height:popts.height}); 
                }
                
				p.panel('panel').animate(positionObj, function(){
                    //鼠标离开自动收起
					$(this).unbind('.layout').bind('mouseleave.layout', {region:region}, function(e){
						if (resizing == true) return;
						collapsePanel(container, e.data.region);
                        //proxyImg.css({'zIndex':4});
					});
				});
				
				return false;
			});
		}
		
		var copts = getOption();
		if (!isVisible(panels[expandP])){
			panels.center.panel('resize', copts.resizeC);
		}
        var proxyImg = $('>div.layout-split-img-' + region, container);
        proxyImg.css({'zIndex':-1}).removeClass(copts.splitImg.toRemoveClass);
		p.panel('panel').animate(copts.collapse, animateSpeed, function(){
			p.panel('collapse',false).panel('close');
			panels[expandP].panel('open').panel('resize', copts.expandP);
            
			//设置split image位置和样式  
            proxyImg.css(copts.splitImg).addClass(copts.splitImg.toAddClass);
                
			$(this).unbind('.layout');
		});

		/**
		 * create expand panel
		 */
		function createExpandPanel(dir){
			var icon;
			if (dir == 'east') icon = 'layout-button-left'
			else if (dir == 'west') icon = 'layout-button-right'
			else if (dir == 'north') icon = 'layout-button-down'
			else if (dir == 'south') icon = 'layout-button-up';
			
			var p = $('<div></div>').appendTo(container);
			p.panel($.extend({}, $.fn.layout.paneldefaults, {
				cls: ('layout-expand layout-expand-' + dir),
				title: '&nbsp;',
				closed: true,
				doSize: false,
				tools: [{
					iconCls: icon,
					handler:function(){
						expandPanel(container, region);
						return false;
					}
				}]
			}));
			p.panel('panel').hover(
				function(){$(this).addClass('layout-expand-over');},
				function(){$(this).removeClass('layout-expand-over');}
			);
			return p;
		}
		
		/**
		 * get collapse option:{
		 *   resizeC:{},
		 *   expand:{},
		 *   expandP:{},	// the expand holder panel
		 *   collapse:{}
		 * }
		 */
		function getOption(){
			var cc = $(container);
			var copts = panels.center.panel('options');
			
			if (region == 'east'){
				var ww = copts.width + popts.width - 33;
                //+1是为了合并边框 border为5的话，就不需要合并两个边框了
				//if (popts.split || !popts.border){ww++;}
				return {
					resizeC:{
						width: ww
					},
					expand:{
						left: cc.width() - popts.width
					},
					expandP:{
						top: copts.top,
						left: cc.width() - 33,
						width: 33,
						height: copts.height
					},
					collapse:{
						left: cc.width(),
						top: copts.top,
						height: copts.height
					},
                    splitImg:{
                        left:cc.width() - 33,
                        zIndex:4,
                        toRemoveClass:'layout-split-right',
                        toAddClass:'layout-split-left'
                    }
				};
			} else if (region == 'west'){
				var ww = copts.width + popts.width - 33;
                //无需合并边框
				//if (popts.split || !popts.border){ww++;}
				return {
					resizeC:{
						width: ww,
						left: 33
					},
					expand:{
						left: 0
					},
					expandP:{
						left: 0,
						top: copts.top,
						width: 33,
						height: copts.height
					},
					collapse:{
						left: -popts.width,
						top: copts.top,
						height: copts.height
					},
                    splitImg:{
                        left:28,
                        zIndex:4,
                        toRemoveClass:'layout-split-left',
                        toAddClass:'layout-split-right'
                    }
				};
			} else if (region == 'north'){
				var hh = copts.height;
				if (!isVisible(panels.expandNorth)){
					//hh += popts.height - 33 + ((popts.split || !popts.border)?1:0);
                    hh += popts.height - 33;
				}
				panels.east.add(panels.west).add(panels.expandEast).add(panels.expandWest).panel('resize', {
					top: 33,
					height: hh
				});

                if(panels.west && panels['layout-split-img-west']){
                    panels['layout-split-img-west'].css({
                        top: Math.floor((hh-35) / 2)
                    });
                }
                if(panels.east && panels['layout-split-img-east']){
                    panels['layout-split-img-east'].css({
                        top: Math.floor((hh-35) / 2)
                    });
                }
				
				return {
					resizeC:{
						top: 33,
						height: hh
					},
					expand:{
						top:0
					},
					expandP:{
						top: 0,
						left: 0,
						width: cc.width(),
						height: 33
					},
					collapse:{
						top: -popts.height,
						width: cc.width()
					},
                    splitImg:{
                        top:28,
                        zIndex:4,
                        toRemoveClass:'layout-split-up',
                        toAddClass:'layout-split-down'
                    }
				};
			} else if (region == 'south'){
				var hh = copts.height;
				if (!isVisible(panels.expandSouth)){
					//hh += popts.height - 33 + ((popts.split || !popts.border)?1:0);
                    hh += popts.height - 33;
				}
				panels.east.add(panels.west).add(panels.expandEast).add(panels.expandWest).panel('resize', {
					height: hh
				});

                var northHeight = 0;
                if(panels.north){//存在north
                    if(isVisible(panels.north)){//north可见
                        northHeight = panels.north.panel('options').height;
                    }else{
                        if(panels.expandNorth){//存在expandNorth
                            if(isVisible(panels.expandNorth)){
                                northHeight = 33;
                            }
                        }
                    }
                }

                if(panels.west && panels['layout-split-img-west']){
                    panels['layout-split-img-west'].css({
                        top:northHeight + Math.floor((hh-35)/2)
                    });
                }
                if(panels.east && panels['layout-split-img-east']){
                    panels['layout-split-img-east'].css({
                        top:northHeight + Math.floor((hh-35)/2)
                    });
                }
				
				return {
					resizeC:{
						height: hh
					},
					expand:{
						top: cc.height()-popts.height
					},
					expandP:{
						top: cc.height() - 33,
						left: 0,
						width: cc.width(),
						height: 33
					},
					collapse:{
						top: cc.height(),
						width: cc.width()
					},
                    splitImg:{
                        top:cc.height() - 33,
                        zIndex:4,
                        toRemoveClass:'layout-split-down',
                        toAddClass:'layout-split-up'
                    }
				};
			}
		}
	}
	
	function expandPanel(container, region){
		var panels = $.data(container, 'layout').panels;
		
		var p = panels[region];
		var popts = p.panel('options');
		if (popts.onBeforeExpand.call(p) == false){return;}
        
        var borderStyleObj = {};
        if (region == 'north') {
            borderStyleObj.borderBottomWidth = '';
        } else if (region == 'south') {
            borderStyleObj.borderTopWidth = '';
        } else if (region == 'east') {
            borderStyleObj.borderLeftWidth = '';
        } else {
            borderStyleObj.borderRightWidth = '';
        }
        p.panel('panel').css(borderStyleObj);
        
        var proxyImg = $('>div.layout-split-img-' + region, container);
        proxyImg.css({'zIndex':-1});
		
		var eopts = getOption();
        proxyImg.removeClass(eopts.splitImg.toRemoveClass);
		var expandP = 'expand' + region.substring(0,1).toUpperCase() + region.substring(1);
		if (panels[expandP]){
			panels[expandP].panel('close');
			p.panel('panel').stop(true,true);
			p.panel('expand',false).panel('open').panel('resize', eopts.collapse);
			p.panel('panel').animate(eopts.expand, function(){
				setSize(container);
                proxyImg.addClass(eopts.splitImg.toAddClass).css({'zIndex':4});
			});
		}
		
		/**
		 * get expand option: {
		 *   collapse:{},
		 *   expand:{}
		 * }
		 */
		function getOption(){
			var cc = $(container);
			var copts = panels.center.panel('options');
			
			if (region == 'east' && panels.expandEast){
				return {
					collapse:{
						left: cc.width(),
						top: copts.top,
						height: copts.height
					},
					expand:{
						left: cc.width() - panels['east'].panel('options').width
					},
                    splitImg:{
                        zIndex:4,
                        toRemoveClass:'layout-split-left',
                        toAddClass:'layout-split-right'
                    }
				};
			} else if (region == 'west' && panels.expandWest){
				return {
					collapse:{
						left: -panels['west'].panel('options').width,
						top: copts.top,
						height: copts.height
					},
					expand:{
						left: 0
					},
                    splitImg:{
                        toRemoveClass:'layout-split-right',
                        toAddClass:'layout-split-left'
                    }
				};
			} else if (region == 'north' && panels.expandNorth){
				return {
					collapse:{
						top: -panels['north'].panel('options').height,
						width: cc.width()
					},
					expand:{
						top: 0
					},
                    splitImg:{
                        toRemoveClass:'layout-split-down',
                        toAddClass:'layout-split-up'
                    }
				};
			} else if (region == 'south' && panels.expandSouth){
				return {
					collapse:{
						top: cc.height(),
						width: cc.width()
					},
					expand:{
						top: cc.height()-panels['south'].panel('options').height
					},
                    splitImg:{
                        toRemoveClass:'layout-split-up',
                        toAddClass:'layout-split-down'
                    }
				};
			}
		}
	}
	
//	function bindEvents(container){
//		var panels = $.data(container, 'layout').panels;
//		var cc = $(container);
//		
//		// bind east panel events
//		if (panels.east.length){
//			panels.east.panel('panel').bind('mouseover','east',_collapse);
//		}
//		
//		// bind west panel events
//		if (panels.west.length){
//			panels.west.panel('panel').bind('mouseover','west',_collapse);
//		}
//		
//		// bind north panel events
//		if (panels.north.length){
//			panels.north.panel('panel').bind('mouseover','north',_collapse);
//		}
//		
//		// bind south panel events
//		if (panels.south.length){
//			panels.south.panel('panel').bind('mouseover','south',_collapse);
//		}
//		
//		panels.center.panel('panel').bind('mouseover','center',_collapse);
//		
//		function _collapse(e){
//			if (resizing == true) return;
//			
//			if (e.data != 'east' && isVisible(panels.east) && isVisible(panels.expandEast)){
//				collapsePanel(container, 'east');
//			}
//			if (e.data != 'west' && isVisible(panels.west) && isVisible(panels.expandWest)){
//				collapsePanel(container, 'west');
//			}
//			if (e.data != 'north' && isVisible(panels.north) && isVisible(panels.expandNorth)){
//				collapsePanel(container, 'north');
//			}
//			if (e.data != 'south' && isVisible(panels.south) && isVisible(panels.expandSouth)){
//				collapsePanel(container, 'south');
//			}
//			return false;
//		}
//	}
	
	function isVisible(pp){
		if (!pp) return false;
		if (pp.length){
			return pp.panel('panel').is(':visible');
		} else {
			return false;
		}
	}
	
	function initCollapse(container){
		var panels = $.data(container, 'layout').panels;
		if (panels.east.length && panels.east.panel('options').collapsed) {
			collapsePanel(container, 'east', 0);
		}
		if (panels.west.length && panels.west.panel('options').collapsed) {
			collapsePanel(container, 'west', 0);
		}
		if (panels.north.length && panels.north.panel('options').collapsed) {
			collapsePanel(container, 'north', 0);
		}
		if (panels.south.length && panels.south.panel('options').collapsed) {
			collapsePanel(container, 'south', 0);
		}
	}
	
	$.fn.layout = function(options, param){
		if (typeof options == 'string'){
			return $.fn.layout.methods[options](this, param);
		}
		
		options = options || {};
		return this.each(function(){
			var state = $.data(this, 'layout');
			if (state){
				$.extend(state.options, options);
			} else {
				var opts = $.extend({}, $.fn.layout.defaults, $.fn.layout.parseOptions(this), options);
				$.data(this, 'layout', {
					options: opts,
					panels: {center:$(), north:$(), south:$(), east:$(), west:$()}
				});
				init(this);
//				bindEvents(this);
			}
			setSize(this);
			initCollapse(this);
		});
	};
	
	$.fn.layout.methods = {
		resize: function(jq){
			return jq.each(function(){
				setSize(this);
			});
		},
		panel: function(jq, region){
			return $.data(jq[0], 'layout').panels[region];
		},
		collapse: function(jq, region){
			return jq.each(function(){
				collapsePanel(this, region);
			});
		},
		expand: function(jq, region){
			return jq.each(function(){
				expandPanel(this, region);
			});
		},
		add: function(jq, options){
			return jq.each(function(){
				addPanel(this, options);
				setSize(this);
				if ($(this).layout('panel', options.region).panel('options').collapsed){
					collapsePanel(this, options.region, 0);
				}
			});
		},
		remove: function(jq, region){
			return jq.each(function(){
				removePanel(this, region);
				setSize(this);
			});
		}
	};
	
	$.fn.layout.parseOptions = function(target){
		return $.extend({}, $.parser.parseOptions(target,[{fit:'boolean'}]));
	};
	
	$.fn.layout.defaults = {
		fit: false
	};
	
	$.fn.layout.parsePanelOptions = function(target){
		var t = $(target);
		return $.extend({}, $.fn.panel.parseOptions(target), 
				$.parser.parseOptions(target, [
					'region',{split:'boolean',splitImg:'false',minWidth:'number',minHeight:'number',maxWidth:'number',maxHeight:'number'}
				]));
	};
	
	$.fn.layout.paneldefaults = $.extend({}, $.fn.panel.defaults, {
		region:null,	// possible values are: 'north','south','east','west','center'
		split:false,
        splitImg:false,
		minWidth:10,
		minHeight:10,
		maxWidth:10000,
		maxHeight:10000
	});
})(jQuery);
